<?php
session_start();
class Admin_artice extends CI_Controller{
    private $per_page = 4;
    private $total_rows =100;
    function  __construct(){
        parent::__construct();
        $this->load->model('mod_artice/mmod_artice');
    }
    function index(){
	}
    function view_list($param = ''){
        $select = array('id','iddanhmuc','title','order');
        $offset = $this->uri->segment(5,0); // sua cai nay
        if($param != ''){
            $where = array('publish'=>1,'iddanhmuc'=>$param);
            $order = array('truong'=>'order','sapxep'=>'DESC');
        } 
        else{
            $where = array('publish'=>1);
            $order = array('truong'=>'id','sapxep'=>'DESC');
        }
        $this->total_rows = $this->home_model->get_number('baiviet',$where);
        if ($offset > $this->total_rows){ 
            $offset = 0;
        }
        $data['artice'] = $this->home_model->get_data('baiviet',$where ,$order,$this->per_page,$select,$offset);
        $data['danhmuc'] = $this->home_model->get_data('danhmuc',array(),array(),'',array('id','title','alias'));
         
        $this->phantrang();
        $this->load->view('mod_artice/vadmin_artice',$data);
    }
    function edit_artice($param){
        $_SESSION['KCFINDER'] = array();
        $_SESSION['KCFINDER']['disabled'] = false; 
        $this->load->library(array('form_validation'));
        $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[10]|xss_clean');
        $this->form_validation->set_rules('alias', 'Alias', 'trim|required|min_length[10]|xss_clean');
        $this->form_validation->set_rules('iddanhmuc', 'Danh Mục', 'trim|required|xss_clean');
        $this->form_validation->set_rules('publish', 'Đăng', 'trim|required|min_length[1]|xss_clean');
        $this->form_validation->set_rules('metades', 'Miêu tả', 'xss_clean');
        $this->form_validation->set_rules('metakey', 'Từ khóa', 'xss_clean');
        $this->form_validation->set_rules('introtext', 'Intro', 'trim|required|min_length[10]|max_length[300]|xss_clean');
        $this->form_validation->set_rules('fulltext', 'Nội Dung', 'trim|required|min_length[10]|xss_clean');

        if ($this->form_validation->run() == FALSE){}
        else{
            $baiviet = $_POST;
            $baiviet['image'] = $this->laydiachianh($_POST['fulltext']);
            $this->mmod_artice->update_artice($param,$baiviet);
        }
        $data['baiviet'] = $this->home_model->get_one('baiviet',array('id'=>$param));
        $data['danhmuc'] = $this->home_model->get_data('danhmuc',array('publish'=>1));
        $this->load->view('mod_artice/vadmin_edit_artice',$data);
    }
    function add_artice(){
        $_SESSION['KCFINDER'] = array();
        $_SESSION['KCFINDER']['disabled'] = false; 
        $this->load->library(array('form_validation'));
        $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[10]|xss_clean');
        $this->form_validation->set_rules('alias', 'Alias', 'trim|required|min_length[10]|xss_clean');
        $this->form_validation->set_rules('iddanhmuc', 'Danh Mục', 'trim|required|xss_clean');
        $this->form_validation->set_rules('publish', 'Đăng', 'trim|required|min_length[1]|xss_clean');
        $this->form_validation->set_rules('metades', 'Miêu tả', 'xss_clean');
        $this->form_validation->set_rules('metakey', 'Từ khóa', 'xss_clean');
        $this->form_validation->set_rules('introtext', 'Intro', 'trim|required|min_length[10]|max_length[300]|xss_clean');
        $this->form_validation->set_rules('fulltext', 'Nội Dung', 'trim|required|min_length[10]|xss_clean');
        
        $data['baiviet'] = array();
        if ($this->form_validation->run() == FALSE){}
        else{
            
            $baiviet = $_POST;
            if(!$this->home_model->get_one('baiviet',array('alias'=>$baiviet['alias']))){
                $baiviet['order'] = $this->home_model->get_number('baiviet',array('iddanhmuc'=>$_POST['iddanhmuc']))+1;
                $baiviet['image'] = $this->laydiachianh($_POST['fulltext']);
                $resuilt = $this->mmod_artice->add_artice($baiviet);
                redirect('backend/admin/artice/edit-'.$resuilt->id); 
            }else{
                $data['thongbao'] = '<p style="color:#f00;font-weight:bold">Đã có bài viết cùng tên</p>';
            }
            
        }
        
        $data['danhmuc'] = $this->home_model->get_data('danhmuc',array('publish'=>1));
        $this->load->view('mod_artice/vadmin_add_artice',$data);
    }
    function delete_artice($param){
        $this->db->delete('baiviet',array('id'=>$param));
        redirect(base_url().'backend/admin/artice/all');
    }
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = base_url().'backend/admin/artice/'.$this->uri->segment(4,0);
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 5;
        $config['num_links'] = 12;
        
        $this->pagination->initialize($config);
    }
    function laydiachianh($image){        
            $src_start = strpos($image, 'src="') + 5;
            $src_end = strpos($image, '"', $src_start);
            $src = substr($image, $src_start, $src_end - $src_start);
            return $src;
    }
    function xulyajax(){
        $data = array();
        for($i = 1;$i <= count($_POST)/2;$i++){
            $data[$i-1] = array('id'=>$_POST['id'.$i],'order'=>$_POST['order'.$i]);
        }
        $this->db->update_batch('baiviet',$data,'id');
        echo '<p>Đã lưu</p>';
    }
}